package com.witframework.web.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
 * this tag "patches" an input field (or other element) 
 * to use a calendar widget for date selection.
 * @author wuxinyang
 *
 */
public class DateTimePickerTag extends TagSupport{
	private String inputField;
	private String format="%Y-%m-%d";
	private String showTime="false";
	private String timeFormat="24";
	private String onUpdate=null;
	private String button=null;
	private String onSelect=null;
	private String onClose=null;
	private String date=null;
	private String calendar_button=null;
	private String eventName=null;
	private String displayArea=null;
	private String position=null;
	private String singleClick=null;
	private String align=null; 
	public void setInputField(String inputField) {
		this.inputField = inputField;
	}
	public void setFormat(String format) {
		this.format = format;
	}
	public void setShowTime(String showTime) {
		this.showTime = showTime;
	}
	public void setTimeFormat(String timeFormat) {
		this.timeFormat = timeFormat;
	}
	public void setOnUpdate(String onUpdate) {
		this.onUpdate = onUpdate;
	}
	public void setButton(String button) {
		this.button = button;
	}
	public void setOnSelect(String onSelect) {
		this.onSelect = onSelect;
	}
	public void setOnClose(String onClose) {
		this.onClose = onClose;
	}
	public void setDate(String date) {
		this.date = date;
	}
	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
		StringBuffer html=new StringBuffer();
		html.append("<script>");
		html.append("Calendar.setup({");
		html.append("inputField:\""+inputField+"\"");
		html.append(",ifFormat:\""+format+"\"");
		if(showTime.equals("true")){
			html.append(",showsTime :true");
			if(timeFormat.equals("12"))
				html.append(",timeFormat:\"12\",");
			else
				html.append(",timeFormat:\"24\"");
		}
		if(onUpdate!=null)
			html.append(",onUpdate:"+onUpdate);
		if(onSelect!=null)
			html.append(",onSelect:"+onSelect); 
		if(onClose!=null)
			html.append(",onClose:"+onClose); 
		if(date!=null)
			html.append(",date:"+date); 
		if(eventName!=null)
			html.append(",eventName:\""+eventName+"\""); 
		if(displayArea!=null)
			html.append(",displayArea:\""+displayArea+"\""); 
		if(position!=null)
			html.append(",position:"+position);
		if(singleClick!=null)
			html.append(",singleClick:"+singleClick);
		if(align!=null)
			html.append(",align:\""+align+"\"");
		if(button!=null){
			if(button.equals("default")){
				String img_button_id="img"+new java.util.Random().nextInt();
				calendar_button="<img src=\""+request.getContextPath()+"/webresource/components/calendar/calendar.gif\" id=\""+img_button_id+"\" style=\"cursor: pointer;\" title=\"日期选择\" />";
				html.append(",button:\""+img_button_id+"\"");
			}
			else
				html.append(",button:\""+button+"\"");
		}
		html.append("});"); 
		html.append("</script>");
		JspWriter writer = pageContext.getOut();
		try {
			if(calendar_button!=null)
				writer.println(calendar_button);
			writer.println(html);
		} catch (IOException e) {
			throw new JspException("Print/Flush in LoadingTag tag error:" + e);
		}
		return (EVAL_BODY_INCLUDE);
	}
	public void setEventName(String eventName) {
		this.eventName = eventName;
	}
	public void setDisplayArea(String displayArea) {
		this.displayArea = displayArea;
	}
	public void setPosition(String position) {
		this.position = position;
	}
	public void setSingleClick(String singleClick) {
		this.singleClick = singleClick;
	}
	public void setAlign(String align) {
		this.align = align;
	}
}
